import mitt from 'mitt';

class Message{
    constructor(){
        this.event=mitt();
        this.init();
    }
    //初始化监听事件
    init(){
       /*  window.JSBridge.onReceiveNativeMessage=(msg)=>{
            const {type,body}=msg;
            console.log(msg)
            this.event.emit(type,body)
        };
        console.log("window.JSBridge:",window.JSBridge) */
        window.addEventListener('message',(msg)=>{
            const {type,body}=msg.data;
            this.event.emit(type,body)
        },false);

    }
    //监听接收消息
    receive(type,callback){
        this.event.on(type,callback);
    }
    //往原生层发送消息
    send(msg){
        //父页面发送消息
        window.parent.postMessage(msg,'*');
    }
}

export default new Message();